﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GUI.PhanQuyen
{
    public partial class frmQuanly : Form
    {
        public frmQuanly()
        {
            InitializeComponent();
        }

        Entities.TaiKhoan[] atk;
        Entities.NhomQuyen[] anq;
        Entities.ChiTietQuyen[] actq;

        private void frmQuanly_Load(object sender, EventArgs e)
        {
            LayTaiKhoan();
            LayNhomQuyen();
            AddTreeView();
        }

        #region Event
        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }



        private void btnThemTK_Click(object sender, EventArgs e)
        {
            frmXuLyTaiKhoan frm = new frmXuLyTaiKhoan();
            frm.anq = anq;
            frm.themTK = new frmXuLyTaiKhoan.AddTK(AddTK);
            frm.ShowDialog();
        }

        private void btnSuaTK_Click(object sender, EventArgs e)
        {
            if (tvNhomQuyen.SelectedNode == null)
                return;
            if (tvNhomQuyen.SelectedNode.Parent != null)
            {
                Entities.TaiKhoan tk = (Entities.TaiKhoan)tvNhomQuyen.SelectedNode.Tag;
                frmXuLyTaiKhoan frm = new frmXuLyTaiKhoan(tk);
                frm.anq = anq;
                frm.SuaTK = new frmXuLyTaiKhoan.UpdateTK(UpdateTK);
                frm.ShowDialog();
            }
            else
            {
                MessageBox.Show("Phải chọn 1 tài khoản");
            }
        }

        private void btnXoaTK_Click(object sender, EventArgs e)
        {
            if (tvNhomQuyen.SelectedNode == null)
                return;
            if (tvNhomQuyen.SelectedNode.Parent != null)
            {
                Entities.TaiKhoan tk = (Entities.TaiKhoan)tvNhomQuyen.SelectedNode.Tag;
                int i = Common.StaticVariable.InterfaceObject.DeleteTaiKhoan(tk);
                if (i == 1)
                {
                    Entities.TaiKhoan[] ataikhoan = new Entities.TaiKhoan[atk.Length - 1];
                    for (int y = 0; y < ataikhoan.Length; y++)
                    {
                        if (tk.TenDangNhap != atk[y].TenDangNhap)
                            ataikhoan[y] = atk[y];
                    }
                    atk = ataikhoan;
                    AddTreeView();
                }
                else
                {
                    MessageBox.Show("Xóa thất bại");
                }
            }
            else
            {
                MessageBox.Show("Phải chọn 1 tài khoản");
            }
        }

        private void btnThemquyen_Click(object sender, EventArgs e)
        {
            TreeNode tn = (TreeNode)tvNhomQuyen.SelectedNode;
            if (tn == null)
                return;
            if (tn.Parent == null)
            {
                frmThemquyen frm = new frmThemquyen();
                frm.tennhomquyen = tn.Name;
                frm.actq = actq;
                frm.ThemCTQ = new frmThemquyen.AddCTQ(AddCTQ);
                frm.ShowDialog();
            }
            
        }
        private void tvNhomQuyen_Click(object sender, EventArgs e)
        {
            
        }

        private void btnThemnhomquyen_Click(object sender, EventArgs e)
        {
            frmXuLyNhomQuyen frm = new frmXuLyNhomQuyen();
            frm.anq = anq;
            frm.ThemNQ = new frmXuLyNhomQuyen.AddNQ(AddNQ);
            frm.ShowDialog();
        }

        private void btnCapnhatquyen_Click(object sender, EventArgs e)
        {
            Entities.ChiTietQuyen[] achitietquyen = (Entities.ChiTietQuyen[])dgvHienThi.DataSource;

            bool c = Common.StaticVariable.InterfaceObject.UpdateChiTietQuyen(achitietquyen, Common.StaticVariable.User);
        }

        private void btnXoaNhomQuyen_Click(object sender, EventArgs e)
        {
            TreeNode tn = tvNhomQuyen.SelectedNode;
            if (tn != null)
            {
                if (tn.Parent == null)
                {
                    Entities.NhomQuyen nq = (Entities.NhomQuyen)tn.Tag;
                    if (nq.isDeleted)
                    {
                        MessageBox.Show("Nhóm Này không được phép xóa");

                    }
                    else
                    {
                        int i = Common.StaticVariable.InterfaceObject.DeleteNhomQuyen(nq);

                        LayTaiKhoan();
                        LayNhomQuyen();
                        AddTreeView();

                        dgvHienThi.DataSource = null;
                    }
                }
            }
        }

        #endregion
        #region Method
        public void LayTaiKhoan()
        {
            DataTable dt = Common.StaticVariable.InterfaceObject.SelectTaiKhoan();
            if (dt == null)
            {
                atk = new Entities.TaiKhoan[0];
            }
            else
            {
                atk = new Entities.TaiKhoan[dt.Rows.Count];
                int i = 0;
                foreach (DataRow item in dt.Rows)
                {
                    Entities.TaiKhoan tk = new Entities.TaiKhoan();
                    tk.TenDangNhap = item[0].ToString();
                    tk.MatKhauDangNhap = item[1].ToString();
                    tk.KhoaTaiKhoan = bool.Parse(item[2].ToString());
                    tk.TenNhanVien = item[3].ToString();
                    tk.MaNhanVien = item[4].ToString();
                    tk.Administrator = bool.Parse(item[5].ToString());
                    tk.TenNhomQuyen = item[6].ToString();

                    atk[i++] = tk;
                }
            }
        }

        void LayNhomQuyen()
        {
            DataTable dt = Common.StaticVariable.InterfaceObject.SelectNhomQuyen();
            if (dt == null)
            {
                anq = new Entities.NhomQuyen[0];
                return;
            }
            else
            {
                anq = new Entities.NhomQuyen[dt.Rows.Count];
                int i = 0;
                foreach (DataRow item in dt.Rows)
                {
                    Entities.NhomQuyen nq = new Entities.NhomQuyen();
                    nq.NhomQuyenId = int.Parse(item[0].ToString());
                    nq.TenNhomQuyen = item[1].ToString();
                    nq.isDeleted = bool.Parse(item[2].ToString());

                    anq[i++] = nq;
                }
            }
        }

        void LayChiTietQuyen(string tennhomquyen)
        {
            DataTable dt = Common.StaticVariable.InterfaceObject.SelectChiTietQuyen(tennhomquyen);

            if (dt == null)
            {
                actq = new Entities.ChiTietQuyen[0];
            }
            else
            {
                actq = new Entities.ChiTietQuyen[dt.Rows.Count];

                int i = 0;
                foreach (DataRow item in dt.Rows)
                {
                    Entities.ChiTietQuyen ctq = new Entities.ChiTietQuyen();

                    ctq.TenNhomQuyen = item[1].ToString();
                    ctq.TenForm = (item[2].ToString());
                    ctq.Ten = (item[3].ToString());
                    ctq.QuyenThem = bool.Parse(item[4].ToString());
                    ctq.QuyenSua = bool.Parse(item[5].ToString());
                    ctq.QuyenXoa = bool.Parse(item[6].ToString());
                    ctq.QuyenXem = bool.Parse(item[7].ToString());

                    actq[i++] = ctq;
                }
            }

            if (actq == null)
            {
                actq = new Entities.ChiTietQuyen[0];
            }
            dgvHienThi.DataSource = actq;
            FixGridView();
        }

        void FixGridView()
        {
            dgvHienThi.Columns["TenNhomQuyen"].HeaderText = "Tên nhóm quyền";
            dgvHienThi.Columns["TenForm"].Visible = false;
            dgvHienThi.Columns["Ten"].HeaderText = "Tên";
            dgvHienThi.Columns["QuyenThem"].HeaderText = "Quyền thêm";
            dgvHienThi.Columns["QuyenSua"].HeaderText = "Quyền sửa";
            dgvHienThi.Columns["QuyenXoa"].HeaderText = "Quyền xóa";
            dgvHienThi.Columns["QuyenXem"].HeaderText = "Quyền xem";
        }
        void AddTreeView()
        {
            tvNhomQuyen.Nodes.Clear();
            if (anq != null)
            {
                foreach (Entities.NhomQuyen item in anq)
                {
                    TreeNode tn = new TreeNode(item.TenNhomQuyen);
                    tn.Name = item.TenNhomQuyen;
                    tn.Tag = item.isDeleted;
                    if (atk != null)
                    {
                        Entities.TaiKhoan[] tk = atk.Where(p => p.TenNhomQuyen == tn.Name).ToArray<Entities.TaiKhoan>();
                        foreach (Entities.TaiKhoan sitem in tk)
                        {
                            TreeNode stn = new TreeNode(sitem.TenDangNhap);
                            stn.Name = sitem.TenDangNhap;
                            stn.Tag = sitem;

                            tn.Nodes.Add(stn);
                        }
                    }
                    tvNhomQuyen.Nodes.Add(tn);
                }
            }
        }
        #endregion

        #region Delegate

        #region TaiKhoan
        public void AddTK(Entities.TaiKhoan tk)
        {
            Entities.TaiKhoan[] ataikhoan = new Entities.TaiKhoan[atk.Length+1];
            for (int i = 0; i < atk.Length; i++)
            {
                ataikhoan[i] = atk[i];
            }
            ataikhoan[atk.Length] = tk;
            atk = ataikhoan;
            AddTreeView();
        }
        public void UpdateTK(Entities.TaiKhoan tk)
        {
            for (int i = 0; i < atk.Length; i++)
            {
                if (atk[i].TenDangNhap == tk.TenDangNhap)
                {
                    atk[i] = tk;
                    break;
                }
            }
            AddTreeView();
        }
        #endregion

        #region NhomQuyen
        public void AddNQ(Entities.NhomQuyen nq)
        {
            Entities.NhomQuyen[] anhomquyen = new Entities.NhomQuyen[anq.Length + 1];
            for (int i = 0; i < atk.Length; i++)
            {
                anhomquyen[i] = anq[i];
            }
            anhomquyen[atk.Length] = nq;
            anq = anhomquyen;
            AddTreeView();
        }
        #endregion
        #region ChiTietQuyen
        public void AddCTQ(Entities.ChiTietQuyen ctq)
        {

            Entities.ChiTietQuyen[] achititequyen = new Entities.ChiTietQuyen[actq.Length + 1];
            for (int i = 0; i < actq.Length; i++)
            {
                achititequyen[i] = actq[i];
            }
            achititequyen[actq.Length] = ctq;
            actq = achititequyen;

            dgvHienThi.DataSource = actq;

            FixGridView();
        }
        #endregion

        private void tvNhomQuyen_AfterSelect(object sender, TreeViewEventArgs e)
        {
            TreeNode tv = tvNhomQuyen.SelectedNode;
            if (tv == null)
            {
                return;
            }
            if (tv.Parent == null)
            {
                LayChiTietQuyen(tv.Name);
            }
            else
            {
            }
        }

        

        
        #endregion

    }
}
